package com.a01_2021.thousandstimes.saas.service.impl;

import com.a01_2021.thousandstimes.general.entity.AbilityModel;
import com.a01_2021.thousandstimes.general.mapper.AbilityModelMapper;
import com.a01_2021.thousandstimes.instance.entity.Position;
import com.a01_2021.thousandstimes.instance.entity.RecruitInfo;
import com.a01_2021.thousandstimes.instance.mapper.PositionMapper;
import com.a01_2021.thousandstimes.instance.mapper.RecruitInfoMapper;
import com.a01_2021.thousandstimes.saas.entity.OpenRecruitment;
import com.a01_2021.thousandstimes.saas.entity.vo.OpenRecruitQuery;
import com.a01_2021.thousandstimes.saas.mapper.OpenRecruitmentMapper;
import com.a01_2021.thousandstimes.saas.service.OpenRecruitmentService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@Service
public class OpenRecruitmentServiceImpl extends ServiceImpl<OpenRecruitmentMapper, OpenRecruitment> implements OpenRecruitmentService {

    @Autowired
    private OpenRecruitmentMapper openRecruitmentMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private AbilityModelMapper abilityModelMapper;
    @Autowired
    private RecruitInfoMapper recruitInfoMapper;

    @Override
    public OpenRecruitment isPosition(OpenRecruitment openRecruitment) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (openRecruitment != null) {
            if (openRecruitment.getInstanceId() != null) {
                queryWrapper.eq("instance_id", openRecruitment.getInstanceId());
            }
            if (openRecruitment.getPositionName() != null) {
                queryWrapper.eq("position_name", openRecruitment.getPositionName());
            }
            queryWrapper.eq("state", openRecruitment.getState());
        }
        OpenRecruitment openRecruitment1 =openRecruitmentMapper.selectOne(queryWrapper);
        return openRecruitment1;
    }


    //实例数据库插入岗位信息
    @Override
    @DS("#header.instanceid")
    public AjaxResponse insertRecruit(OpenRecruitQuery openRecruitQuery) {
        OpenRecruitment openRecruitment = openRecruitQuery.getOpenRecruitment();
        //插position表
        Position position = new Position();
        position.setPostId(openRecruitment.getPostId());
        position.setName(openRecruitment.getPositionName());
        position.setDescription(openRecruitment.getDescription());
        positionMapper.insert(position);
        //插ability_model表
        AbilityModel abilityModel = new AbilityModel();
        abilityModel.setPostId(openRecruitment.getPostId());
        abilityModel.setPositionId(position.getId());
        abilityModel.setAbility1Weight(openRecruitQuery.getAbility1Weight());
        abilityModel.setAbility2Weight(openRecruitQuery.getAbility2Weight());
        abilityModel.setAbility3Weight(openRecruitQuery.getAbility3Weight());
        abilityModel.setAbility4Weight(openRecruitQuery.getAbility4Weight());
        abilityModel.setAbility5Weight(openRecruitQuery.getAbility5Weight());
        abilityModel.setAbility6Weight(openRecruitQuery.getAbility6Weight());
        abilityModel.setAbility7Weight(openRecruitQuery.getAbility7Weight());
        abilityModel.setAbility8Weight(openRecruitQuery.getAbility8Weight());
        abilityModelMapper.insert(abilityModel);
        //插recruit_info表
        RecruitInfo recruitInfo = new RecruitInfo();
        recruitInfo.setPositionId(position.getId());
        recruitInfo.setHr(openRecruitment.getHr());
        recruitInfo.setCity(openRecruitment.getCity());
        recruitInfo.setCompany(openRecruitment.getCompany());
        recruitInfo.setRequirement(openRecruitment.getRequirement());
        recruitInfo.setNumber(openRecruitment.getNumber());
        recruitInfo.setStartTime(openRecruitment.getStartTime());
        recruitInfo.setSalary(openRecruitment.getSalary());
        recruitInfoMapper.insert(recruitInfo);
        return AjaxResponse.success("插入实例数据库成功");
    }


    //主数据库插入岗位信息
    @Override
    @DS("master")
    public AjaxResponse insertOpenRecruitment(OpenRecruitQuery openRecruitQuery) {
        OpenRecruitment openRecruitment1 = openRecruitQuery.getOpenRecruitment();
        OpenRecruitment openRecruitment2 = this.isPosition(openRecruitment1);
        if (openRecruitment2==null){
            //HR之前没有发布过这个岗位
            openRecruitment1.setStartTime(LocalDateTime.now());
            openRecruitment1.setState(1);
            openRecruitmentMapper.insert(openRecruitment1);
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", openRecruitment1.getId());
            return AjaxResponse.success(resultMap);
        }else {
            AjaxResponse resultBean = AjaxResponse.error("您已经发布过该岗位");
            return resultBean;
        }
    }

    @Override
    public AjaxResponse getOpenRecruitment(Integer state) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",state);
        List<OpenRecruitment> openRecruitment = openRecruitmentMapper.selectList(queryWrapper);
        return AjaxResponse.success(openRecruitment);
    }

    @Override
    public AjaxResponse getOpenRecruitmentByInstance(Long instanceId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("instance_id",instanceId);
        List<OpenRecruitment> openRecruitment = openRecruitmentMapper.selectList(queryWrapper);
        return AjaxResponse.success(openRecruitment);
    }


    @Override
    public AjaxResponse getOpenRecruitmentByHr(String hr) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("hr",hr);
        List<OpenRecruitment> openRecruitment = openRecruitmentMapper.selectList(queryWrapper);
        return AjaxResponse.success(openRecruitment);
    }

    @Override
    public AjaxResponse setOpenRecruitmentState(Integer id) {
        OpenRecruitment openRecruitment = openRecruitmentMapper.selectById(id);
        if(openRecruitment!=null){
            openRecruitment.setState(0);
            openRecruitment.setOverTime(LocalDateTime.now());
            openRecruitmentMapper.updateById(openRecruitment);
            return AjaxResponse.success(openRecruitment);
        }else{
            return AjaxResponse.error("该岗位不存在");
        }
    }


}
